/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.models.quizzes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import org.brainstretch.models.User;
import org.brainstretch.models.UserQuiz;
import org.brainstretch.recommend.QuizProfile;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 *
 * @author mrodaitis
 */
@Entity
@XmlRootElement
@NamedQueries({@NamedQuery(name="Quiz.maxDuration", query="SELECT MAX(quiz.duration) FROM Quiz quiz"),
                @NamedQuery(name="Quiz.getAttempts", query="SELECT uq FROM UserQuiz uq JOIN uq.quiz quiz WHERE quiz.id = ?1 AND uq.status != ?2"),
                @NamedQuery(name="Quiz.count", query="SELECT COUNT(quiz) FROM Quiz quiz")
                })
public class Quiz implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final Category DEFAULT_CATEGORY = Category.Wordplay;
    
    public Quiz() {
        duration = 5;
        category = DEFAULT_CATEGORY;
        creationDate = new Date();        
        questionSets = new ArrayList<QuestionSet>();
        attempts = 0;
        profile = new QuizProfile(this);
    }
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable=false)
    private Date creationDate;
    
    @ManyToOne()
    private User creator;
    
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private List<QuestionSet> questionSets;
    
    @Column(nullable = false)
    private String title;
    
    @Column(nullable = false)
    private boolean hidden;
    
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private Category category;
    
    @Column(nullable = false)
    private int duration;
    
    @Column(nullable = false)
    private int attempts;

    @OneToOne(cascade= CascadeType.ALL)
    private QuizProfile profile;

    public QuizProfile getProfile() {
        return profile;
    }

    public void setProfile(QuizProfile profile) {
        this.profile = profile;
    }
    
    public int getAttempts() {
        return attempts;
    }

    public void setAttempts(int attempts) {
        this.attempts = attempts;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
    
    @JsonIgnore
    public boolean isHidden() {
        return hidden;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }
    
    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @JsonIgnore
    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    @JsonIgnore
    public List<QuestionSet> getQuestionSets() {
        return questionSets;
    }

    public void setQuestionSets(List<QuestionSet> questionSets) {
        this.questionSets = questionSets;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }    
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }   
    
    public void calculateQuizMetrics(){
        profile.calculateQuizMetrics(this);
    }
    
    public void calculateUserMetrics(List<UserQuiz> newData){
        profile.calculateUserMetrics(newData);
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Quiz)) {
            return false;
        }
        Quiz other = (Quiz) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.brainstretch.models.quizzes.Quiz[ id=" + id + " ]";
    }
    
}
